$(document).ready(function() {
    $.ajaxSetup({
        data: {csrfmiddlewaretoken: '{{ csrf_token }}' }
    });
    var login_email="" ;
    var login_password="" ;
    var login_status=false ;
    $(".input-box").val("");
    $(".input-box").bind('input propertychange', function() { //给获取input元素绑定监听事件
        var $this = $(this);
        /*var text_length = $this.val().length;// 获取当前文本框的长度
        var singe_len = $(".input-box").outerWidth(true)/text_length ;
        var current_width = parseInt(text_length) * 19;*/
        var width=textWidth($(this).val());
        $(this).width(width) ;
    }) ;
    $(document).click(function () {
        $(".input-box").focus();
        if(true==login_status) {
            $(".input-box").attr("type","password") ;
        } else {
            $(".input-box").attr("type","text") ;
        }
    }) ;
    $(document).keydown(function(e){ 
        //alert(e.keyCode);  //获取当前按键的键码
        if (true==login_status) {
            $(".input-box").attr("type","password") ;
        } else {
            $(".input-box").attr("type","text") ;
        }
        if (e.keyCode==13) {
            var cmd = $(".input-box").val() ;
            if (true!=login_status) {
                var new_cmd=""
                new_cmd += " <li class=\"normal-code\">\
                    <span class=\"user-code\">star@boogieLing_o</span>\
                    <span class=\"normal-code\">:</span> \
                    <span class=\"other-code\">~</span>\
                    <span class=\"normal-code\">$</span>\
                    <span class=\"run-code\">"+cmd+"</span>\
                </li>" ;
                $(".main-box").append(new_cmd) ;
                var scrollHeight = $(".main-box").prop("scrollHeight");
                $(".main-box").animate({scrollTop:scrollHeight}, 400);
                $(".input-box").val("").width("5px") ;
            } else {}
            if (true==login_status) {
                login_password=cmd ;
                $.ajax({
                   type:"POST",
                   data: { 
                           email:login_email,
                           password:login_password
                          },
                   url: curUrl, 
                   //这里用的是static url 需要与urls.py中的name一致
                   cache: false,
                   dataType: "html",
                   success: function(result, status, xml) {
                        if (result) {
                            var jsonData = JSON.parse(result);
                            $("#login-password ~ span").text(jsonData['message']) ;
                            var new_cmd=""
                            new_cmd += "<li class=\"normal-code\">\
                                <span class=\"error-code\">LoginError</span>\
                                <span class=\"normal-code\">\""+jsonData['message']+"\".</span>\
                                </li>" ;
                            $(".main-box").append(new_cmd) ;
                            //window.location.reload() ;
                        } else {
                            window.open(curIndex) ;
                        }
                    },
                    error: function(xhr, textStatus, errorThrown){
                        alert("error:pls call the admin "+xhr.responseText);
                    }
                  });
                login_status=false ;
                var scrollHeight = $(".main-box").prop("scrollHeight");
                $(".main-box").animate({scrollTop:scrollHeight}, 400);
                $(".input-box").val("").width("5px") 
                return false ;
            } else {
            }
            if ("help"==cmd) {
                var new_cmd=""
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">contact</span>\
                                <span class=\"normal-code\">//Return a list of my contact information.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">skills</span>\
                                <span class=\"normal-code\">// Return a list of my skills and my rating of them.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">hobby</span>\
                                <span class=\"normal-code\">// Return a list of my hobbies.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">sudo</span>\
                                <span class=\"normal-code\">// Log in to your account.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">radio</span>\
                                <span class=\"normal-code\">// Open my personal radio.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">echo</span>\
                                <span class=\"normal-code\">// Echoes input.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">restart</span>\
                                <span class=\"normal-code\">// Introducting myself again.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">blog</span>\
                                <span class=\"normal-code\">// Information about the blog.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">resume</span>\
                                <span class=\"normal-code\">// Open my resume in a new tab.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">stat</span>\
                                <span class=\"normal-code\">// View information for an article.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">cd</span>\
                                <span class=\"normal-code\">// Jump to an article.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">all-post</span>\
                                <span class=\"normal-code\">// Return a list of all article titles.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">clr</span>\
                                <span class=\"normal-code\">// Clear command line.</span>\
                                </li>" ;
                $(".main-box").append(new_cmd) ;
            } else if (cmd.indexOf("sudo")!=-1) {
                if ("True"==user_status) { 
                    var new_cmd=""
                    new_cmd += "<li class=\"normal-code\">\
                                    <span class=\"error-code\">SystemError</span>\
                                    <span class=\"normal-code\">Already logged in.</span>\
                                    </li>" ;
                    $(".main-box").append(new_cmd) ;
                } else {
                    var new_cmd=""
                    login_email=cmd.replace("sudo","") ;
                    login_email=login_email.replace(" ","") ;
                    new_cmd += "<li class=\"system-code\">\
                                     <span class=\"done-code\">System</span>\
                                     <span class=\"normal-code\">Your email :"+login_email+"</span>\
                                     </li>" ;
                    new_cmd += "<li class=\"system-code\">\
                                     <span class=\"done-code\">System</span>\
                                     <span class=\"normal-code\">Enter password...</span>\
                                     </li>" ;
                    $(".main-box").append(new_cmd) ;
                    login_status=true ;
                }
            } else if ("contact"==cmd) {
                var new_cmd=""
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"tag-code\">Email:</span>\
                                <span class=\"normal-code\">boogieLing_o@163.com</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"tag-code\">Email:</span>\
                                <span class=\"normal-code\">boogieLing_o@qq.com</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"tag-code\">Wechat:</span>\
                                <span class=\"normal-code\">boogieLing_o</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"tag-code\">Github:</span>\
                                <span class=\"normal-code\">https://github.com/boogieLing</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"tag-code\">QQ:</span>\
                                <span class=\"normal-code\">1654262479</span>\
                                </li>" ;
                $(".main-box").append(new_cmd) ;
            } else if ("skills"==cmd){
                var new_cmd="";
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">A:</span>\
                                <span class=\"normal-code\">CPP11 ▂▂▂▃▃▃▅▅▅▆▇▇▊▊ 80/100</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">A:</span>\
                                <span class=\"normal-code\">CPP14 ▂▂▂▃▃▃▅▅▅▆▇▊ 70/100</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">A:</span>\
                                <span class=\"normal-code\">C ▂▂▂▃▃▃▅▅▅▆▇▇▊▊ 80/100</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">A:</span>\
                                <span class=\"normal-code\">PYTHON3 ▂▂▃▃▃▅▆▇▇▊ 70/100</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"ce-code\">B:</span>\
                                <span class=\"normal-code\">HTML5 ▂▂▂▃▃▃▅▆▇▇▊▊▊ 80/100</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"ce-code\">B:</span>\
                                <span class=\"normal-code\">JS EC6 ▂▂▂▃▃▃▅▆▇▇ 70/100</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"ce-code\">B:</span>\
                                <span class=\"normal-code\">TypeScript ▂▂▂▃▆▇ 40/100</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"ce-code\">B:</span>\
                                <span class=\"normal-code\">CSS ██████████████████████████████ 1e9/100</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"ce-code\">B:</span>\
                                <span class=\"normal-code\">JAVA ▂▂▃▅▅▇▊ 60/100</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"ce-code\">B:</span>\
                                <span class=\"normal-code\">PHP ▂▂▃▅▅▇▊ 60/100</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"ce-code\">B:</span>\
                                <span class=\"normal-code\">Shell ▂▂▃▃▅▅▇▇▊ 70/100</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"error-code\">C:</span>\
                                <span class=\"normal-code\">Linux ▂▂▂▃▃▅▅▅▇▇▊▊ 75/100</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"error-code\">C:</span>\
                                <span class=\"normal-code\">Draw ███████████████████████████████████████████ 1e20/100</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"error-code\">C:</span>\
                                <span class=\"normal-code\">printDebug ███████████████████████████████████████████ 1e20/100</span>\
                                </li>" ;
                 new_cmd += "<li class=\"normal-code\">\
                                <span class=\"error-code\">C:</span>\
                                <span class=\"normal-code\">Sport ----------------------0/100</span>\
                                </li>" ;
                $(".main-box").append(new_cmd) ;
            } else if ("hobby"==cmd) {
                var new_cmd=""
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"ce-code\">calligraphy</span>\
                                <span class=\"normal-code\">Learn calligraphy from the age of six.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"ce-code\">sketch</span>\
                                <span class=\"normal-code\">Worked as an art student for five years.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"ce-code\">ACGN</span>\
                                <span class=\"normal-code\">にじげんDaiisuki!.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"error-code\">Coding</span>\
                                <span class=\"normal-code\">Code is my life.</span>\
                                </li>" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"error-code\">Sleeping</span>\
                                <span class=\"normal-code\">Sleep to keep alive.</span>\
                                </li>" ;
                $(".main-box").append(new_cmd) ;
            } else if (cmd.indexOf("echo")!=-1)  {
                var tmp=cmd.replace("echo","") ;
                tmp=tmp.replace(" ","") ;
                var new_cmd="" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"system-code\">echo</span>\
                                <span class=\"normal-code\">"+tmp+"</span>\
                                </li>" ;
                 $(".main-box").append(new_cmd) ;
            } else if ("restart"==cmd)  {
                window.location.reload() ;
            } else if ("blog"==cmd)  {
                var new_cmd="" ;
                new_cmd+="\
                    <li class=\"normal-code\">\
                        <span class=\"time-code\">2020/08/19</span>\
                        <span class=\"tag-code\">About Blog:</span>\
                        <span class=\"normal-code\">Here are some things to say:</span>\
                        <ul class=\"ul-code\" >\
                            <li class=\"normal-code\">关于这个博客</li>\
                            <li class=\"normal-code\">About this blog</li>\
                            <li class=\"normal-code\">其实是二次重构的版本了</li>\
                            <li class=\"normal-code\">It's actually a refactored version</li>\
                            <li class=\"normal-code\">旧博客存放在https://plsseer0qaq.top</li>\
                            <li class=\"normal-code\">The old blog is stored at https://plsseer0qaq.top</li>\
                            <li class=\"normal-code\">初次制作：2018/07/17 thinkphp+wordpress</li>\
                            <li class=\"normal-code\">First production: 2018/07/17 thinkphp+wordpress</li>\
                            <li class=\"normal-code\">因为使用了大量的外来模板</li>\
                            <li class=\"normal-code\">Because of the use of a large number of foreign templates</li>\
                            <li class=\"normal-code\">而且感觉年纪大了也不喜欢花里胡哨的东西了。</li>\
                            <li class=\"normal-code\">And I don’t like bells and whistles when I am older.</li>\
                            <li class=\"normal-code\">所以我进行了重构</li>\
                            <li class=\"normal-code\">So I refactored it</li>\
                            <li class=\"normal-code\">目前版本：2020/08/19 Django</li>\
                            <li class=\"normal-code\">Current version: 2020/08/19 Django</li>\
                            <li class=\"normal-code\">现在的小站可以说是真正一行代码一行代码敲出来的。</li>\
                            <li class=\"normal-code\">The current station can be said to have been knocked out by myself.</li>\
                            <li class=\"normal-code\">css3,html5,js EC6 ▊▊▊▊▊▊▊▊▊▊▊▊▊▊▊▊▊▊ 100/100</li>\
                            <li class=\"normal-code\">测试系统 ▊▊▊▊▊▊▊▊▊▊▊▊▊▊▊▊▊▊ 100/100</li>\
                            <li class=\"normal-code\">表单系统 ▊▊▊▊▊▊▊▊▊ 50/100</li>\
                            <li class=\"normal-code\">(果然还是喜欢完全由自己造出来的东西啊a'w'a)</li>\
                            <li class=\"normal-code\">(Sure enough, I still like things made entirely by myself a'w'a)</li>\
                            <li class=\"normal-code\">(感觉没有很好发挥Django的特性0x0)</li>\
                            <li class=\"normal-code\">(I don't feel that Django's features are not well used 0x0)</li>\
                            <li class=\"normal-code\">再一次感受到了前端后端测试运维全栈的滋味。</li>\
                            <li class=\"normal-code\">Once again feel the taste of the whole stack.</li>\
                            <li class=\"normal-code\">如果在小站遇到了bug，欢迎与站长联系，感谢。</li>\
                            <li class=\"normal-code\">If you encounter a bug in the small station, please contact the webmaster, thanks.</li>\
                        </ul>\
                    </li>\
                    " ;
                 $(".main-box").append(new_cmd) ;
            } else if ("exit"==cmd)  {
                window.location=curIndex ;
            } else if ("resume"==cmd)  {
                var new_cmd="" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"error-code\">PageError</span>\
                                <span class=\"normal-code\">"+"The page is still under development."+"</span>\
                                </li>" ;
                 $(".main-box").append(new_cmd) ;
            } else if ("radio"==cmd)  {
                var new_cmd="" ;
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">DONE</span>\
                                <span class=\"normal-code\">"+"Please enjoy the music."+"</span>\
                                </li>" ;
                $(".main-box").append(new_cmd) ;
                window.open("https://www.plsseer0qaq.top") ;
            } else if (cmd.indexOf("stat")!=-1) {
                var post_title=cmd.replace("stat","") ;
                post_title=post_title.replace(" ","") ;
                $.ajax({
                   type:"POST",
                   data: { 
                           post_title:post_title,
                          },
                   url: findUrl, 
                   //这里用的是static url 需要与urls.py中的name一致
                   cache: false,
                   dataType: "html",
                   success: function(result, status, xml) {
                        if (result) {
                            var jsonData = JSON.parse(result);
                            var new_cmd=""
                            new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">"+"Title"+"</span>\
                                <span class=\"normal-code\">\""+jsonData["title"]+"\".</span>\
                                </li>" ;
                            new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">"+"Author"+"</span>\
                                <span class=\"normal-code\">\""+jsonData["author"]+"\".</span>\
                                </li>" ;
                            new_cmd += "<li class=\"normal-code\">\
                                <span class=\"ce-code\">"+"Cate"+"</span>\
                                <span class=\"normal-code\">\""+jsonData["cate"]+"\".</span>\
                                </li>" ;
                            new_cmd += "<li class=\"normal-code\">\
                                <span class=\"ce-code\">"+"Createtime"+"</span>\
                                <span class=\"normal-code\">\""+jsonData["creat_time"]+"\".</span>\
                                </li>" ;
                            for (var tag of jsonData["tag_list"]) {
                                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"tag-code\">"+"Tag"+"</span>\
                                <span class=\"normal-code\">\""+tag+"\".</span>\
                                </li>" ;
                            }
                            $(".main-box").append(new_cmd) ;
                            //window.location.reload() ;
                        } else {
                            var new_cmd=""
                            new_cmd += "<li class=\"normal-code\">\
                                <span class=\"error-code\">"+"PageError"+"</span>\
                                <span class=\"normal-code\">"+post_title+": Post not found."+"</span>\
                                </li>" ;
                            $(".main-box").append(new_cmd) ;
                        }
                    },
                    error: function(xhr, textStatus, errorThrown){
                        alert("error:pls call the admin "+xhr.responseText);
                    }
                  });
            } else if (cmd.indexOf("cd")!=-1) {
                var post_title=cmd.replace("cd","") ;
                post_title=post_title.replace(" ","") ;
                $.ajax({
                   type:"POST",
                   data: { 
                           post_title:post_title,
                          },
                   url: cdUrl, 
                   //这里用的是static url 需要与urls.py中的name一致
                   cache: false,
                   dataType: "html",
                   success: function(result, status, xml) {
                        if (result) {
                            var jsonData = JSON.parse(result);
                            var new_cmd=""
                            new_cmd += "<li class=\"normal-code\">\
                                <span class=\"done-code\">"+"DONE"+"</span>\
                                <span class=\"normal-code\">"+post_title+" already opened."+"</span>\
                                </li>" ;
                            $(".main-box").append(new_cmd) ;
                            window.open(jsonData["success_url"]) ;
                        } else {
                            var new_cmd=""
                            new_cmd += "<li class=\"normal-code\">\
                                <span class=\"error-code\">"+"PageError"+"</span>\
                                <span class=\"normal-code\">"+post_title+": Post not found."+"</span>\
                                </li>" ;
                            $(".main-box").append(new_cmd) ;
                            //window.location.reload() ;
                        }
                    },
                    error: function(xhr, textStatus, errorThrown){
                        alert("error:pls call the admin "+xhr.responseText);
                    }
                  });
            } else if ("all-post"==cmd) {
                $.ajax({
                   type:"POST",
                   data: { 
                           post_title:post_title,
                          },
                   url: allUrl, 
                   //这里用的是static url 需要与urls.py中的name一致
                   cache: false,
                   dataType: "html",
                   success: function(result, status, xml) {
                        if (result) {
                            var jsonData = JSON.parse(result);
                            var new_cmd=""
                            for( var post of jsonData["post_list"]){
                                new_cmd += "<li class=\"normal-code\">\
                                    <span class=\"tag-code\">"+post["category"]+"</span>\
                                    <span class=\"normal-code\">"+post["title"]+"</span>\
                                    </li>" ;
                            }
                            $(".main-box").append(new_cmd) ;
                        } else {
                            var new_cmd=""
                            new_cmd += "<li class=\"normal-code\">\
                                <span class=\"error-code\">"+"PageError"+"</span>\
                                <span class=\"normal-code\">"+post_title+": Post not found."+"</span>\
                                </li>" ;
                            $(".main-box").append(new_cmd) ;
                            //window.location.reload() ;
                        }
                    },
                    error: function(xhr, textStatus, errorThrown){
                        alert("error:pls call the admin "+xhr.responseText);
                    }
                  });
            } else if ("clr"==cmd)  {
                $(".main-box").text("") ;
            }
            else  { 
                var new_cmd=""
                new_cmd += "<li class=\"normal-code\">\
                                <span class=\"error-code\">CommandError</span>\
                                <span class=\"normal-code\">\""+cmd+"\" is not recognized as an internal or external command</span>\
                                </li>" ;
                $(".main-box").append(new_cmd) ;
            }
        } else {}
    }); 
})
var textWidth = function(text){
    var sensor = $('<pre>'+ text +'</pre>').css({display: 'none'});
    $('body').append(sensor);
    var width = sensor.width();
    sensor.remove();
    return width+10;
};